<template>
  <div ref="chartRef" :style="{ height, width }"></div>
</template>
<script lang="ts">
  import { defineComponent,onMounted, PropType, ref, Ref, reactive, watchEffect } from 'vue';
  import { useECharts } from '/@/hooks/web/useECharts';
  import { cloneDeep } from 'lodash-es';
  export default defineComponent({
    name: 'bar',
    props: {
      chartData: {
        type: Array,
        default: () => [],
      },
      option: {
        type: Object,
        default: () => ({}),
      },
      width: {
        type: String as PropType<string>,
        default: '100%',
      },
      height: {
        type: String as PropType<string>,
        default: '280px',
      },
      // update-begin--author:liaozhiyang---date:20240407---for：【QQYUN-8762】首页默认及echars颜色调整
      seriesColor: {
        type: String,
        default: '#1890ff',
      },
      // update-end--author:liaozhiyang---date:20240407---for：【QQYUN-8762】首页默认及echars颜色调整
    },
    setup(props) {
      const chartRef = ref<HTMLDivElement | null>(null);
      const { setOptions, resize } = useECharts(chartRef as Ref<HTMLDivElement>);
         onMounted(()=>{
          window.addEventListener('resize',function(){
            resize()
          })
        })
      let current = props.chartData.count; // 当前用量
      let all = 100; // 总量
      const option = reactive({
        series: [
          {
            type: 'pie',
            label: {
              show: false,
            },
            center: ['50%', '50%'],
            radius: ['80%', '90%'],
            hoverAnimation: false,
            startAngle: 180,
            data: [
              {
                name: '用量',
                value: current,
                itemStyle: {
                  color: '#1990ff',
                },
              },
              {
                name: 'rest', // 实际显示部分是总量-用量
                value: all - current,
                itemStyle: {
                  color: '#e5e5e5',
                },
              },
              {
                name: 'bottom',
                value: all,
                itemStyle: {
                  color: 'transparent',
                },
              },
            ],
          },
        ],
      });

      watchEffect(() => {
        props.chartData && initCharts();
      });

      function initCharts() {
        if (props.option) {
          Object.assign(option, cloneDeep(props.option));
        }

        setOptions(option);
      }
      return { chartRef };
    },
  });
</script>
